const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

// 当前运行环境
const env = process.env.NODE_ENV
const is_dev = env === 'dev'

function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  devtool: is_dev ? "#eval-source-map" : false,

  entry: {
    app: './src/main.js'
  },
  output: {
    path: resolve('dist/'),
    filename: 'static/js/[name].[hash].js'
  },

  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      'src': resolve('src'),
      '@': resolve('src/components'),
      'assets': resolve('src/assets'),
      'static': resolve('src/static')
    }
  },

  module: {
    rules: [
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader?modules'
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.json$/,
        use: 'json-loader'
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 100000,
          name: 'static/img/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.styl$/,
        exclude: /(node_modules)/,
        loader: 'style!css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]__[hash:base64:5]!postcss!stylus-loader'
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/fonts/[name].[hash:7].[ext]'
        }
      }
    ]
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html',
      inject: true
    }),
    new webpack.HotModuleReplacementPlugin(),//webpack热加载插件
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, './src/static'),
        to: 'static/img',
        ignore: ['.*']
      }
    ])
  ],

  devServer: {
    historyApiFallback: true,
    inline: true,
    hot: true,
    open: true,
    noInfo: true,
    port: 8082
  }
}
